<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>核酸工作站结果文件上传</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 50px;
        }
        .container {
            max-width: 600px;
            margin: auto;
            text-align: center;
        }
        input[type="file"] {
            margin: 20px 0;
        }
        #fileList {
            margin: 10px 0;
            color: #555;
        }
        #uploadBtn {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }
        #uploadBtn:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>核酸工作站结果文件上传</h1>

    <!-- File Upload Form -->
    <input type="file" id="fileInput" multiple>
    <div id="fileList"></div>
    <button id="uploadBtn">上传</button>

    <div id="status"></div>
</div>

<script>
    // 获取文件输入元素
    const fileInput = document.getElementById('fileInput');
    const fileList = document.getElementById('fileList');
    const uploadBtn = document.getElementById('uploadBtn');
    const status = document.getElementById('status');

    // 监听文件选择事件
    fileInput.addEventListener('change', function () {
        const files = fileInput.files;  // 获取选择的文件列表
        fileList.innerHTML = '';  // 清空之前的文件列表

        // 显示文件名
        for (let i = 0; i < files.length; i++) {
            const listItem = document.createElement('p');
            listItem.textContent = `File: ${files[i].name} (${(files[i].size / 1024).toFixed(2)} KB)`;
            fileList.appendChild(listItem);
        }
    });

    // 监听上传按钮点击事件
    uploadBtn.addEventListener('click', function () {
        if (fileInput.files.length === 0) {
            status.textContent = "请选择要上传的文件！";
            return;
        }

        const formData = new FormData();
        const files = fileInput.files;

        // 将文件添加到 FormData 对象
        for (let i = 0; i < files.length; i++) {
            formData.append('file', files[i]);
        }

        // 使用 fetch API 发送 POST 请求
        fetch('http://192.168.11.3:80/openApi/api/data/nucleicAcidWorkstation/upload', {
            method: 'POST',
            body: formData
        })
            .then(response => {
                if (response.ok) {
                    return response.json();
                } else {
                    throw new Error('上传失败');
                }
            })
            .then(result => {
                status.textContent = '文件上传成功！';
                console.log('Success:', result);
            })
            .catch(error => {
                status.textContent = 'Error uploading file: ' + error.message;
                console.error('Error:', error);
            });
    });
</script>
</body>
</html>
